#include "../header/datagram_header.hpp"

#include "../header/common_conf.hpp"

datagram_header::datagram_header(datagram_type type){
    this->type = type;

    this->dataLength = 0;
    this->seqNum = 1;
    this->follow = 0;
    this->rcv_window_size = reciver_window_size;
}

// use to read header in buffer
datagram_header::datagram_header(char* buffer){
    this->type = (datagram_type)(buffer[3]);
    this->dataLength = buffer[0]+128+(buffer[1]+128)*256;
    this->seqNum = buffer[4]+128+(buffer[5]+128 + (buffer[6]+128)*256)*256;
    this->follow = buffer[2];
    this->rcv_window_size = buffer[7]+128;
    // printf("HEADER seq:%d\nBuffer:%d:%d",seqNum,buffer[4]+128,buffer[5]+128);
}

// make header a string in buffer
void datagram_header::get(char* buffer){
    // first line for datalength(2) ack,and  datagram_type
    buffer[0]=dataLength%256-128;
    buffer[1]=dataLength/256-128;
    buffer[2]=follow;
    buffer[3]=type;
    // second line for seqnum and rcv_window_size
    buffer[4]=seqNum%256-128;
    buffer[5]=seqNum/256%256 -128;
    buffer[6]=seqNum/256/256 -128;
    buffer[7]=rcv_window_size-128;
}